<?
  include "../common/db_conn.php";

  /*
    ptype
      0: 안내문 저장
      1: 대분류 저장
  */
  
  $ptype = $_REQUEST["ptype"];
  $notice = $_POST["notice"]; 
  
  $notice = addslashes($notice);
  echo("ptype[$ptype]<br>");
  echo("notice[$notice]<br>");
  
  if ($ptype == 0) {  // 안내문 저장
    
    $query = "update sv_base set sb_text = '$notice' where sb_idx = 1 limit 1";
    $result = mysql_query($query);
  
    if ($result) {
      echo("<script>alert('Success');location.href='about:blank';</script>");
    } else {
      echo("<script>alert('Error');location.href='about:blank';</script>");   
    }
  } else if ($ptype == 1) { // 대분류 저장
    for ($i = 0; $i < 5; $i++) {
      $sd_idx = $i + 1;
      $sd_text = addslashes($_POST["d_".$i]);

      echo("sd_idx[$sd_idx] sd_text[$sd_text]<br>");
      
      $query = "update sv_dlist set sd_text = '$sd_text' where sd_idx = $sd_idx limit 1";
      $result = mysql_query($query);
    } 

    if ($result) {
      echo("<script>alert('Success');location.href='about:blank';</script>");
    } else {
      echo("<script>alert('Error');location.href='about:blank';</script>");   
    }
  } else if ($ptype == 2) { // 문제 저장
    // sv_qlist - sq_text, sq_type, sq_answer
    // sv_alist - sq_idx, sa_text (delete before insert)
  
    $sq_idx = $_POST["sq_idx"];
    $sd_idx = $_POST["sd_idx"];
    $sq_text = addslashes($_POST["sq_text"]);
    $sq_type = $_POST["sq_type"];
    $a_test = $_POST["a_test"];
    $sq_answer = $_POST["sq_answer"];
    $count = $_POST["count"];
    
    $sq_from = $_POST["sq_from"];
    $sq_to = $_POST["sq_to"];

    if ($a_test >= 1 || $sq_type != 0) {
      $sq_answer = 0;
    }
    
    if ($a_test != 2 || $sq_type != 0) {
      $sq_from = "NULL";
      $sq_to = "NULL";
    } 
    
    echo("sq_type[$sq_type] sq_text[$sq_text] sq_answer[$sq_answer] count[$count]<br>");
    
    if ($sq_idx > 0) {
      $query_q = "update sv_qlist set sq_text = '$sq_text', sq_type = $sq_type, sq_from = $sq_from, sq_to = $sq_to, sq_answer = $sq_answer where sq_idx = $sq_idx limit 1";
      $result_q = mysql_query($query_q);
    } else {
      // sq_no 구하기
      $query_q = "select count(*) cnt from sv_qlist where sd_idx = $sd_idx";
      $result_q = mysql_query($query_q);
      $rows_q = mysql_fetch_array($result_q);
      $sq_no = $rows_q["cnt"] + 1;
      
      $query_q = "insert into sv_qlist (sd_idx, sq_no, sq_text, sq_type, sq_from, sq_to, sq_answer) values ($sd_idx, $sq_no, '$sq_text', $sq_type, $sq_from, $sq_to, $sq_answer)";
      echo("$query_q<br>");
      $result_q = mysql_query($query_q);
      $sq_idx = mysql_insert_id();
    }
    
    $query_a = "delete from sv_alist where sq_idx = $sq_idx limit 10";  
    $result_a = mysql_query($query_a);
    
    if ($sq_type == 0) {    
      for ($i = 0; $i < $count; $i++) {
        $sa_text = addslashes($_POST["a".($i+1)]);
          
        echo($i.": sa_text[$sa_text]<br>");

        $query_a = "insert into sv_alist (sq_idx, sa_text) values ($sq_idx, '$sa_text')";
        $result_a = mysql_query($query_a);
      }   
    }
  } else if ($ptype == 3) { // 문제 순서 변경
    $sd_idx = $_POST["sd_idx"];
    $change_from = $_POST["change_from"];
    $change_to = $_POST["change_to"];
    
    $query_q = "select sq_idx from sv_qlist where  sd_idx = $sd_idx and sq_no = $change_from limit 1";
    $result_q = mysql_query($query_q);
    $rows_q = mysql_fetch_array($result_q);
    $sq_idx_change_from = $rows_q["sq_idx"];
    echo("change_from[$change_from] change_to[$change_to] sq_idx_change_from: $sq_idx_change_from<br>");
    
    if ($change_from > $change_to) {
      // change_to ~ change_from-1은 1씩 증가
      // change_from이었던 것을 change_to로 보냄 (윗 작업을 하기 전에 기억해 놔야 함)
      $query_q = "update sv_qlist set sq_no = sq_no + 1 where sd_idx = $sd_idx and sq_no >= $change_to and sq_no < $change_from";   
      $result_q = mysql_query($query_q);
    } else if ($change_from < $change_to) {
      // change_from+1 ~ change_to은 1씩 감소
      // change_from였던 것은 change_to로 보냄 (윗 작업을 하기 전에 기억해 놔야 함)   
      $query_q = "update sv_qlist set sq_no = sq_no - 1 where  sd_idx = $sd_idx and sq_no > $change_from and sq_no <= $change_to";    
      $result_q = mysql_query($query_q);
    }
    echo("query_q[$query_q]<br>");
    
    $query_q = "update sv_qlist set sq_no = $change_to where sq_idx = $sq_idx_change_from limit 1";   
    $result_q = mysql_query($query_q);
    
    echo("<script>alert('Successfully moved');parent.document.location.reload();</script>");
  } else if ($ptype == 4) { // 문제 삭제
    // 문제 삭제 후 sq_no 업데이트
    
    $sq_idx = $_GET["sq_idx"];
    $sd_idx = $_GET["sd_idx"];

    echo("delete question<br>sq_idx[$sq_idx] sd_idx[$sd_idx]<br>");

    $query_q = "select sq_no from sv_qlist where sq_idx = $sq_idx limit 1";
    $result_q = mysql_query($query_q);
    $rows_q = mysql_fetch_array($result_q);
    $sq_no_deleted = $rows_q["sq_no"];
  
    $query_q = "delete from sv_qlist where sq_idx = $sq_idx limit 1";   
    $result_q = mysql_query($query_q);
    
    $query_q = "update sv_qlist set sq_no = sq_no - 1 where sq_no >= $sq_no_deleted and sd_idx = $sd_idx";    
    $result_q = mysql_query($query_q);
    
    echo("<script>alert('Successfully moved');parent.document.location.reload();</script>");
  }

  include "../common/db_close.php";
?>
